//
//  ChargeCardView.m
//  aiteshangcheng
//
//  Created by caominglei on 2018/4/24.
//  Copyright © 2018年 aite. All rights reserved.
//

#import "ChargeCardView.h"
#import "WealthyCell.h"
#import "WealthyModel.h"
#import "WealthyView.h"
#import "Network.h"
#import "UIView+NoDataShow.h"
@interface ChargeCardView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (nonatomic , strong) UIImageView *noDataIV;
@property (strong, nonatomic) WealthyView *wealthyView;

@end
@implementation ChargeCardView
static NSString *wealthycell = @"wealthycell";


- (WealthyView *)wealthyView
{
    if (!_wealthyView) {
        _wealthyView = [[WealthyView alloc] init];
    }
    return _wealthyView;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WealthyCell class] forCellReuseIdentifier:wealthycell];
    }
    return _tableView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.dataArray = [NSMutableArray array];

        [self getWealthySerialList];
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self createSubviews];
        [self createSubviewsFrame];
    }
    return self;
}

/**
 key 会员登录key
 当前页curpage 若无此参数默认1；
 */
- (void)getWealthySerialList
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"key"] = DHNetworkManager.shareManager.userAccount.key;
    dic[@"curpage"] = @"1";
    NSString *url = [NSString stringWithFormat:@"%@%@",REMOTEURL,Rcb_log_lis];
    [Network executeGetPredepositTaskWithUrl:url Dict:dic Success:^(id obj) {
        NSArray *dataArray = [NSArray yy_modelArrayWithClass:[WealthyModel class] json:obj[@"datas"][@"list"]];
        self.amount.text = [NSString stringWithFormat:@"可用金额:%@,冻结金额:%@",obj[@"datas"][@"member_info"][@"available_predeposit"],obj[@"datas"][@"member_info"][@"freeze_predeposit"]];
        [self.dataArray addObjectsFromArray:dataArray];
        if (self.dataArray.count == 0) {
            
            self.noDataIV = [UIView NoDataImg:@"nopicture" withTip:@"您当前还没有相关记录哦~T_T~"];
            self.noDataIV.center = CGPointMake(kScreenWidth * 0.5, 200);
            [self addSubview:self.noDataIV];
            self.tableView.hidden = YES;
        }else{
            self.tableView.hidden = NO;
            
            [self.tableView reloadData];
        }
    } failed:^(id obj) {
        
    }];
}

- (void)createSubviews
{
    UILabel *amount = [YCCommonCtrl commonLableWithFrame:CGRectZero text:@"可用金额:-/--,冻结金额:-/--" color:[UIColor blackColor] font: [UIFont systemFontOfSize:10] textAlignment:NSTextAlignmentLeft];
    [self addSubview:amount];
    amount.backgroundColor = [UIColor orangeColor];
    self.amount = amount;
    [self addSubview:self.wealthyView];
    [self addSubview:self.tableView];
}

- (void)createSubviewsFrame
{
    [self.amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(40);
    }];
    [self.wealthyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.amount.mas_bottom).offset(5);
        make.height.mas_equalTo(40);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.wealthyView.mas_bottom).offset(5);
        make.height.mas_equalTo(400);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WealthyModel *model = self.dataArray[indexPath.row];
    WealthyCell *cell = [tableView dequeueReusableCellWithIdentifier:wealthycell];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
